package myJqueryOne.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;

import myJqueryOne.entity.CityEntity;
import myJqueryOne.service.CityService;
import myJqueryOne.service.impl.CityServiceImpl;

public class CityServlet extends HttpServlet {
	private static ObjectMapper mapper = new ObjectMapper();
	static CityService cityService = new CityServiceImpl();
//	PrintWriter out = resp.getWriter();
//	List<CityEntity> list = Arrays.asList(new CityEntity(1L,"广东","GD"),new CityEntity(1L,"湖南","HN"));
//	out.write(mapper.writeValueAsString(list));
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");
		System.out.println(method);
		if ("list".equals(method)) {
			list(req, resp);
		}
	}
	private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		PrintWriter out = resp.getWriter();
		out.print(mapper.writeValueAsString(cityService.listAll()));

	}

}